package work_queue;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

public class Consumer2 {
	public static final String QUEUE_NAME = "work_queue";

	// 接收消息
	public static void main(String[] args) throws Exception {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("mq.hinak0.xyz");
		factory.setUsername("admin");
		factory.setPassword("admin");
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();

		/* 消费者成功消费时的回调接口，这里为打印获取到的消息 */
		DeliverCallback deliverCallback = (consumerTag, message) -> {
			System.out.println("接收到的消息:" + new String(message.getBody()));
			channel.basicAck(message.getEnvelope().getDeliveryTag(), false); // 手动应答
		};

		/* 消费者取消消费的回调 */
		CancelCallback callback = consumerTag -> {
			System.out.println("取消消费接口回调逻辑");
		};

		// 接收消息
		channel.basicConsume(QUEUE_NAME, false, deliverCallback, callback);
	}
}
